﻿//=============================================================
// Copyright (C) 2011 深圳市中旭企业管理股份有限公司
// 作 者：江怀玉
// 日 期：2011/10/27
// 描 述: 部门业务处理类
// 版 本：1.00
// 修改历史纪录
// 版 本  修改时间      修改人            修改内容
// 1.00   2011/11/1    曾洋
//                     江怀玉            移动复制功能           
//=============================================================

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ZX.DBModule;
using ZX.EHR.Library;

namespace ZX.BLL
{
    public partial class BDept : BLL.Extend.BaseBussnies<DatDepartment>
    {
        public BDept() {
            this.entity = new DatDepartment();
        }
        /// <summary>
        /// 获取该部门下所有子部门信息
        /// </summary>
        /// <param name="deptPath">部门路径</param>
        /// <returns>IList：DatDepartment</returns>
        public IList<DatDepartment> GetDeptListByID(string deptPath)
        {
            return this.GetEntityListByCondition<DatDepartment>("DeptPath LIKE '" +deptPath + "%'");
        }

        
        //override 
 
        /// <summary>
        /// 创建TreeCode
        /// </summary>
        /// <param name="parentID"></param>
        /// <returns></returns>
        public string CreateNewTreeCode(Guid parentID)
        {
            DatDepartment pDept= this.GetEntityById(parentID.ToString()) as DatDepartment;
            IList<DatDepartment> deptList = this.GetEntityListByCondition<DatDepartment>("ParentID='" + parentID + "'");
            if (deptList == null)
            {
                return pDept.DeptPath + "-001";
            }
            else
            {
                int bDept = deptList.Max(p => p._PathInt);
                if (bDept == 0)
                {
                    return pDept.DeptPath + "-001";
                }
                string _lastCode = (bDept+1).ToString("D3") ;
                return pDept.DeptPath + "-" + _lastCode;
            }
        }

        /// <summary>
        /// 查找部门
        /// </summary>
        /// <param name="Key">搜索Key</param>
        /// <returns></returns>
        public IList<DatDepartment> Find(string Key)
        {
            IList<DatDepartment> deptList = this.GetEntityListByCondition<DatDepartment>(string.Format("LDeptName like '%{0}%' and ExpireDate>=getdate()", Key));
            if (deptList == null || deptList.Count==0)
            {
                return null;
            }
            string _strCondtion = "";
            for (int i = 0; i < deptList.Count; i++)
            {
                _strCondtion = _strCondtion+string.Format("DeptPath like '{0}%' ", deptList[i].DeptPath)+(i==deptList.Count-1?"":" or");

            }
            deptList = this.GetEntityListByCondition<DatDepartment>(string.Format("({0}) and ExpireDate>=getdate()", _strCondtion));
            return deptList.Distinct().OrderBy(p => p.DeptPath).ToList();
            //IList<DatDepartment> deptList = this.GetEntityListByCondition<DatDepartment>(string.Format("DeptName like '%{0}%' and ExpireDate>=getdate()",Key));
        }

        /// <summary>
        /// 获取该部门下所有子总门信息
        /// </summary>
        /// <returns></returns>
        public IList<DatDepartment> GetDeptListByDeptID(Guid? DeptID)
        {
            return this.GetEntitysBySqlComm<DatDepartment>(string.Format("select * from DatDepartment where DeptID in (select id from dbo.fn_GetOrganizeAll('{0}'))", DeptID));
        }


        /// <summary>
        /// 批量更新部门树编码
        /// </summary>
        /// <param name="DeptID">部门ID</param>
        /// <returns></returns>
        public bool BatchUpdateDeptTreeCode(Guid DeptID)
        {
            IList<DatDepartment> _list = GetDeptListByDeptID(DeptID);
            if (_list == null)
                return false;
            // DatDepartment _entity = GetEntityById<DatDepartment>(DeptID);
            //_list.Add(_entity);
            foreach (var entity in _list)
            {
                if (entity.DeptID != DeptID)
                {
                    entity.DeptPath = null;
                    this.Update(entity);
                }
            }
            BatchUpdateDeptTreeCode(DeptID, _list);
            return true;
        }

        /// <summary>
        /// 批量更新部门树编码
        /// </summary>
        /// <param name="DeptID">部门ID</param>
        /// <param name="list">要更新部门列表</param>
        /// <returns></returns>
        public bool BatchUpdateDeptTreeCode(Guid DeptID, IList<DatDepartment> list)
        {
            if (list == null)
            {
                return false;
            }
            var _list = list.Where(p => p.ParentID == DeptID).OrderBy(p => p.Sort);
            if (_list == null)
            {
                return false;
            }
            foreach (var entity in _list)
            {
                entity.DeptPath = CreateNewTreeCode(DeptID);
                this.Update(entity);
                BatchUpdateDeptTreeCode(entity.DeptID, list);
            }
            return true;
        }

        /// <summary>
        /// 部门移动
        /// </summary>
        /// <param name="curDeptID">当前部门</param>
        /// <param name="ParentID"></param>
        /// <returns></returns>
        public bool DeptMove(Guid curDeptID, Guid ParentID)
        {
            DatDepartment curDept = GetEntityByCondition<DatDepartment>("DeptID='" + curDeptID + "'");
            curDept.ParentID = ParentID;
            //curDept.DeptPath = "";
            curDept.DeptPath = CreateNewTreeCode(ParentID);
            this.entity = curDept;
            this.Update();
            //this.entity = curDept;
            BatchUpdateDeptTreeCode(curDept.DeptID);
            return true;
        }


        private IList<DatDepartment> pList = null;
        
        /// <summary>
        /// 部门复制
        /// </summary>
        /// <param name="curDeptID"></param>
        /// <returns></returns>
        
        public bool DeptCopy(Guid curDeptID)
        {
            DatDepartment curDept = GetEntityByCondition<DatDepartment>("DeptID='" + curDeptID + "'");
            ///要复制部门数据
            pList = GetDeptListByDeptID(curDeptID);
            
            Guid SourceID = curDeptID;
            curDept.DeptID = Guid.NewGuid();
            curDept.DeptName = curDept.DeptName + "_New";
            curDept.DeptPath = this.CreateNewTreeCode(curDept.ParentID);
            this.entity = curDept;
            this.Insert(entity);

            foreach (var _entity in pList)
            {
                if (_entity.ParentID == SourceID)
                {
                    _entity.ParentID = curDept.DeptID;
                    _entity.DeptID = Guid.NewGuid();
                    _entity.DeptPath = this.CreateNewTreeCode(_entity.ParentID);
                    this.Insert(_entity);
                    DeptCopy( _entity.DeptID, _entity.DeptID, pList);
                }
            }
            //DeptCopy(curDept.DeptID, curDept.DeptID, list);
            //BatchUpdateDeptTreeCode(curDept.DeptID);
            return true;
        }


        /// <summary>
        /// 部门复制
        /// </summary>
        /// <param name="sourceID"></param>
        /// <param name="newID"></param>
        /// <param name="list"></param>
        /// <returns></returns>
        public bool DeptCopy(Guid sourceID, Guid newID, IList<DatDepartment> list)
        {
            var _list = list.Where(p => p.ParentID == sourceID);

            foreach (var _entity in list)
            {
                if (_entity.ParentID == sourceID)
                {
                    _entity.ParentID = sourceID;
                    _entity.DeptID = Guid.NewGuid();
                    _entity.DeptPath = this.CreateNewTreeCode(_entity.ParentID);
                    this.Insert(_entity);
                    DeptCopy(_entity.DeptID, _entity.DeptID, list);
                }
            }
            //foreach (var entity in _list)
            //{
            //    entity.ParentID = newID;
            //    Guid _sourceID = entity.DeptID;
            //    entity.DeptID = Guid.NewGuid();
            //    this.Insert(entity);
            //    DeptCopy(_sourceID, entity.DeptID, list);
            //}
            return true;
        }
    }
}
